package operations;

import Book.Book;
import Book.Booklist;

import java.util.Scanner;

public class Deleteoperation implements Ioperations{
    @Override
    public void work(Booklist booklist) {
        System.out.println("删除图书中.....");
        System.out.println("请输入要删除的图书:");
        Scanner scanner = new Scanner(System.in);
        String bookname = scanner.nextLine();
        int currentSize = booklist.getUsedSize();
        int pos = 0;
        int i = 0;
        for (; i < currentSize; i++) {
            //找到目标图书
            Book book = booklist.getBooks(i);
            if(book.getBookname().equals(bookname)){
                pos = i;
                break;
            }
        }
        if(i >= currentSize){
            System.out.println("没有你要删除的图书!");
        }
        //开始删除
        for (int j = pos; j < currentSize - 1; j++) {
            //booklist[j] = booklist[j+1]
            Book book = booklist.getBooks(j+1);
            booklist.setBooks(j,book);
        }
        booklist.setUsedSize(currentSize-1);
        booklist.setBooks(currentSize-1,null);
        System.out.println("删除成功！");
    }
}
